const bcrypt = require('bcryptjs');

let User = require("../models/user.js");
let register = function(a,b,z){
	return new Promise((resolve, reject) => {
		User.findOne(a,function(err,data){
			if(err){
				reject({"error": err});
			}else if(data.length != 0){
				reject({"error": "用户名已存在"});
			}else if(data.length == 0){
				bcrypt.genSalt(10, function(err, salt) {
					bcrypt.hash(a, salt, function(err, hash) {
						console.log(hash)
						bcrypt.compare(a, hash, function(err, res) {
							console.log(res)
						});
					})
				})

				let newUser = new User({
					name:a,
					password:b
				});
				newUser.save(function(){
					User.findOne(a,function(err,data){
						resolve(data);
					})
				});
			}
		});
	})
}

module.exports=register;